/*
 * Created on Feb 25, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.ReceiptSearchForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * @author Ravikanth
**/
public class ReceiptSearchCommand extends ChipTrackExtendedAction
{
	/* 
	 * Command to search Receipts
	 */
	public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HashMap errors          = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String search 			= request.getParameter( ACTION_SEARCH );
		String actionForward	= "receiptSearch";
		HttpSession mSession 	= request.getSession();
		ReceiptSearchForm receiptSearchForm = ( ReceiptSearchForm )form;
	
		try
		{
			request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
			request.setAttribute( "yearList", PurchaseOrderDAO.getYearList () );
			request.setAttribute( "productList", PurchaseOrderDAO.loadPOProductsList() );

			if( search != null )
			{
				ArrayList receiptSearchList = PurchaseOrderDAO.searchRecciptOrder( receiptSearchForm );

				if( receiptSearchList != null && receiptSearchList.size() != 0 )
				{
					receiptSearchForm.setReceiptSearchList( receiptSearchList );
				}
				else
				{
					info.put( INFO_RECEIVINGS_TRANSACTIONS_WITH_SEARCH_CRITERIA_NOT_FOUND, null );
				}
			}

			actionForward = "receiptSearch";
		}
		catch ( Exception e )
		{
			e.printStackTrace();
			Logger.error( ERROR_WHILE_EXECUTING_RECEIPT_SEARCH_ACTION_EXECUTE, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_WHILE_EXECUTING_RECEIPT_SEARCH_ACTION_EXECUTE );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
			actionForward = "receiptSearch";
		}

		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
	}
}
